package com.example.movie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.movie.domain.R;
import com.example.movie.service.UserService;
import com.example.movie.domain.User;
import com.example.movie.mapper.UserMapper;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
* @author HuRan
* @description 针对表【user(用户表，用于存储用户的基本信息)】的数据库操作Service实现
* @createDate 2024-10-24 11:09:25
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService {

    @Override
    public R register(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<User>();
        queryWrapper.eq(!user.getName().isEmpty(),User::getName, user.getName());
        if(baseMapper.exists(queryWrapper)){
            return R.error("该用户名已存在！");
        }
//        user.setUPassword(DigestUtils.md5DigestAsHex(user.getUPassword().getBytes()));
        baseMapper.insert(user);
        return R.ok("注册成功！");
    }

    @Override
    public R login(User user, HttpServletRequest request) {
        LambdaQueryWrapper<User> existsWrapper = new LambdaQueryWrapper<User>();
        existsWrapper.eq(!user.getName().isEmpty(),User::getName, user.getName());
        if(!baseMapper.exists(existsWrapper)){
            return R.error("用户不存在！");
        }
        //比对密码
        LambdaQueryWrapper<User> passWordWrapper = new LambdaQueryWrapper<>();
        passWordWrapper.eq(!user.getName().isEmpty(),User::getName, user.getName());
        User selectedUser = baseMapper.selectOne(passWordWrapper);
        if(!selectedUser.getPassword().equals(user.getPassword())){
            return R.error("用户名或者密码错误！");
        }
        //放入session
//        request.getSession().setAttribute("user", selectedUser);

        Map<String,Long> tokenMap = new HashMap<>();
        tokenMap.put("token",selectedUser.getUId());
        return R.ok(tokenMap);
    }

    @Override
    public Boolean isLogin(HttpServletRequest request) {
        //session
//        User sessionUser  = (User)request.getSession().getAttribute("user");
        //token
        String token = request.getHeader("token");
        if(token == null){
            return  false;
        }
        User user = baseMapper.selectById(token);
        if(user == null ){
            return false;
        }
        return true;
    }

    @Override
    public R logout(HttpServletRequest request) {
        request.getSession().setAttribute("user", null);
        return R.ok("用户退出登录成功");
    }

    @Override
    public List<User> getUserListByUserIDS(List<Long> userIDS) {
        if(userIDS == null || userIDS.isEmpty()){
            return Collections.emptyList();
        }
        return baseMapper.selectBatchIds(userIDS);
    }

    @Override
    public R update(User user) {
        if(user.getUId()==null){
            return R.error("参数错误：无userId");
        }
        User selectById = baseMapper.selectById(user.getUId());
        if(selectById == null){
            return R.error("找不到用户");
        }
        if(!selectById.getPassword().equals(user.getPassword())){
            return R.error("请勿用此API更改密码");
        }
        baseMapper.updateById(user);
        return R.ok();
    }

    @Override
    public R changePassword(Long userId, String oldPassword, String newPassword) {
        if(userId==null){
            return R.error("参数错误：无userId");
        }
        User selectById = baseMapper.selectById(userId);
        if(!oldPassword.equals(selectById.getPassword())){
            return R.error("旧密码错误！");
        }
        selectById.setPassword(newPassword);
        baseMapper.updateById(selectById);
        return R.ok();
    }
}




